package com.app.service.impl;


import com.app.mapper.ConfigMapper;
import com.app.mapper.CusEducationMapper;
import com.app.model.ConfigModel;
import com.app.model.CusEducationModel;
import com.app.model.EnterpriseModel;
import com.app.service.ConfigService;
import com.app.service.CusEducationService;
import com.app.service.CustomerService;
import com.app.service.UserService;
import com.app.util.StringTool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @Description:系统配置表
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    ConfigMapper configMapper;
    @Override
    public List<ConfigModel> getListByModel(ConfigModel model) {
        QueryWrapper<ConfigModel> queryWrapper = new QueryWrapper<>();
        if(model.getId()!=null&&model.getId()>0)
        {
            queryWrapper.eq("id", model.getId());
            queryWrapper.or().eq("father_id", model.getId());
        }
        queryWrapper.eq("con_type", model.getConType());
        List<ConfigModel> conList = configMapper.selectList(queryWrapper);
        return conList;
    }

    @Override
    public List<ConfigModel> getGroupListById(Integer id) {
        QueryWrapper<ConfigModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("con_type","2","4");
        queryWrapper.eq("con_level",1);
        if(id!=null&&id>0)
        {
            queryWrapper.eq("id",id);
        }
        List<ConfigModel> conList = configMapper.selectList(queryWrapper);
        return conList;
    }

    @Override
    public ConfigModel getModelByFatherId(Integer id) {
        QueryWrapper<ConfigModel> queryWrapper = new QueryWrapper<>();
        if(id!=null&&id>0)
        {
            queryWrapper.eq("father_id",id);
        }
        List<ConfigModel> conList = configMapper.selectList(queryWrapper);
        return conList.get(0);
    }

    @Override
    public ConfigModel getModelById(Integer id) {
        QueryWrapper<ConfigModel> queryWrapper = new QueryWrapper<>();
        if(id!=null&&id>0)
        {
            queryWrapper.eq("id",id);
        }
        List<ConfigModel> conList = configMapper.selectList(queryWrapper);
        if(conList!=null&&conList.size()>0)
        {
            return conList.get(0);
        }else{
            return null;
        }

    }

    @Override
    public Integer insert(ConfigModel model) {
        return configMapper.insert(model);
    }

    @Override
    public Page<ConfigModel> findByNewsTypePage(String comName, Integer page, Integer limit) {
        QueryWrapper<ConfigModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("con_type",6);
        if (StringTool.isNotEmpty(comName)) {
            queryWrapper.like("name", comName);
        }
        Page<ConfigModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<ConfigModel> configModelPage = configMapper.selectPage(pages, queryWrapper);
        if (configModelPage == null) {
            return null;
        } else {
            for (int i = 0; i < configModelPage.getRecords().size(); i++) {
                if(configModelPage.getRecords().get(i).getFatherId()!=null&&configModelPage.getRecords().get(i).getFatherId()>0)
                {
                    ConfigModel model = getModelById(configModelPage.getRecords().get(i).getFatherId());
                    configModelPage.getRecords().get(i).setFatherIdText(model.getName());
                }
            }
            return configModelPage;
        }
    }

    @Override
    public List<ConfigModel> getGroupListByType(Integer type,Integer fatherId) {
        QueryWrapper<ConfigModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("con_type", type);
        if(fatherId!=null)
        {
            queryWrapper.eq("father_id",fatherId);
        }
        return configMapper.selectList(queryWrapper);
    }

    @Override
    public void delete(Integer id) {
        configMapper.deleteById(id);
    }

    @Override
    public Object villageList() {
        QueryWrapper<ConfigModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("con_type", 2);
        queryWrapper.ne("con_level",3);
        return configMapper.selectList(queryWrapper);
    }
    @Override
    public Object streetList() {
        QueryWrapper<ConfigModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("con_type", 4);
        queryWrapper.ne("con_level",3);
        return configMapper.selectList(queryWrapper);
    }
    @Override
    public ConfigModel getModelByName(String name) {
        QueryWrapper<ConfigModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("con_type", 3);
        queryWrapper.eq("name", name);
        return configMapper.selectOne(queryWrapper);
    }
    @Override
    public List<ConfigModel> selectList(QueryWrapper<ConfigModel> queryWrapper1) {
        return configMapper.selectList(queryWrapper1);
    }
    @Override
    public List<String> getStaticsTown() {
        return this.configMapper.getStaticsTown();
    }

    @Override
    public List<ConfigModel> getStaticsTownId() {
        return this.configMapper.getStaticsTownId();
    }
}
